//
//  MBGoodsDetailCommentView.m
//  MobellCloud_ios
//
//  Created by cherry pu on 2017/12/28.
//  Copyright © 2017年 zhangXin. All rights reserved.
//

#import "MBGoodsDetailCommentView.h"

@interface MBGoodsDetailCommentView ()
@property (nonatomic,weak)UIImageView* headerImageView;
@property (nonatomic,weak)UIImageView* memberImageView;
@property (nonatomic,weak)UILabel* nameLabel;
@property (nonatomic,weak)UILabel* timeLabel;
@property (nonatomic,weak)UILabel* commentLabel;

@property (nonatomic,strong) UIImage* placeHold;
@property (nonatomic,strong)MASConstraint* noImageConstraint;

@property (nonatomic,weak)UILabel* moreLabel;
@property (nonatomic,weak)UILabel* subTitleLabel;

@end

@implementation MBGoodsDetailCommentView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        UIView* allCommentView = [[UIView alloc]init];
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleClickAllCommentButton)];
        [allCommentView addGestureRecognizer:tap];
        [self addSubview:allCommentView];
        
        [allCommentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, [UIView lf_sizeFromIphone6:40]));
        }];

        UILabel* titleLabel = [[UILabel alloc]init];
        titleLabel.text = @"评价";
        titleLabel.font = [UIFont lf_systemFontOfSize:15];
        titleLabel.textColor = [UIColor colorWithR:40 G:40 B:40];
        [allCommentView addSubview:titleLabel];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(allCommentView).with.offset([UIView lf_sizeFromIphone6:15]);
            make.left.equalTo(allCommentView).with.offset([UIView lf_sizeFromIphone6:12]);
        }];
        
        UILabel* subTitleLabel = [[UILabel alloc]init];
        self.subTitleLabel = subTitleLabel;
        subTitleLabel.text = @"(好评率50%)";
        subTitleLabel.font = [UIFont lf_systemFontOfSize:15];
        subTitleLabel.textColor = COLOR_MAIN;
        [allCommentView addSubview:subTitleLabel];
        
        [subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(titleLabel);
            make.left.equalTo(titleLabel.mas_right).with.offset([UIView lf_sizeFromIphone6:12]);
        }];

        UIImageView* nextImageView = [[UIImageView alloc]init];
        nextImageView.image = [UIImage imageNamed:@"home_more"];
        [allCommentView addSubview:nextImageView];
        
        [nextImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(titleLabel);
            make.right.equalTo(allCommentView).with.offset([UIView lf_sizeFromIphone6:-12]);
        }];
        
        UILabel* moreLabel = [[UILabel alloc]init];
        self.moreLabel = moreLabel;
        moreLabel.text = @"共1500条评论";
        moreLabel.font = [UIFont lf_systemFontOfSize:12];
        moreLabel.textColor = [UIColor colorWithR:153 G:153 B:153];
        [allCommentView addSubview:moreLabel];
        
        [moreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(titleLabel);
            make.right.equalTo(nextImageView.mas_left).with.offset([UIView lf_sizeFromIphone6:-12]);
        }];

        
        UIImageView* headerImageView = [[UIImageView alloc]init];
        self.headerImageView = headerImageView;
        headerImageView.image = [UIImage imageFromColor:COLOR_BG forSize:CGSizeMake([UIView lf_sizeFromIphone6:27], [UIView lf_sizeFromIphone6:27]) withCornerRadius:[UIView lf_sizeFromIphone6:27/2]];
        [self addSubview:headerImageView];
        
        [headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom).with.offset([UIView lf_sizeFromIphone6:15]);
            make.left.equalTo(self).with.offset([UIView lf_sizeFromIphone6:12]);
            make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:27], [UIView lf_sizeFromIphone6:27]));
        }];
        
        
        UILabel* nameLabel = [[UILabel alloc]init];
        self.nameLabel = nameLabel;
        nameLabel.text = @"肥肥的龙猫";
        nameLabel.font = [UIFont lf_systemFontOfSize:13];
        nameLabel.textColor = [UIColor colorWithR:40 G:40 B:40];
        [self addSubview:nameLabel];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headerImageView.mas_right).with.offset([UIView lf_sizeFromIphone6:13]);
            make.centerY.equalTo(headerImageView);
        }];
        
        UILabel* timeLabel = [[UILabel alloc]init];
        self.timeLabel = timeLabel;
        timeLabel.text = @"2017-11-11 15:13:39";
        timeLabel.font = [UIFont lf_systemFontOfSize:12];
        timeLabel.textColor = [UIColor colorWithR:153 G:153 B:153];
        [self addSubview:timeLabel];
        
        [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).with.offset([UIView lf_sizeFromIphone6:-12]);
            make.centerY.equalTo(headerImageView);
        }];

        UILabel* commentLabel = [[UILabel alloc]init];
        self.commentLabel = commentLabel;
        commentLabel.numberOfLines = 0;
        [self addSubview:commentLabel];
        
        [commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLabel);
            make.top.equalTo(headerImageView.mas_bottom).with.offset([UIView lf_sizeFromIphone6:10]);
            make.right.equalTo(self).with.offset([UIView lf_sizeFromIphone6:-12]);
        }];
        
        MBHeaderLinesImageContantView* imageContentView = [[MBHeaderLinesImageContantView alloc]initWithImageWidth:(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:26])/3];
        self.imageContantView = imageContentView;
        [self addSubview:imageContentView];
        
        [imageContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            self.noImageConstraint = make.top.equalTo(commentLabel.mas_bottom).with.offset([UIView lf_sizeFromIphone6:10]);
            make.left.equalTo(self).with.offset([UIView lf_sizeFromIphone6:10]);
            make.top.equalTo(commentLabel.mas_bottom).priorityLow();
            make.bottom.equalTo(self).with.offset([UIView lf_sizeFromIphone6:-16]);
        }];
        self.imageContantView.imagePathArray = @[];
        [self.noImageConstraint deactivate];
    }
    return self;
}


-(UIImage *)placeHold
{
    if (_placeHold == nil) {
        _placeHold = [UIImage imageNamed:@"moren"];
    }
    return _placeHold;
}


-(void)setCommentModel:(MBCommentModel *)commentModel
{
    _commentModel = commentModel;
    
    NSString* imageUrl = commentModel.head_pic;
    if (![imageUrl containsString:@"http"]) {
        imageUrl = [NSString stringWithFormat:@"%@%@",BASE_IMAGE_URL,commentModel.head_pic];
    }

    
    UIImage* image = [[MBCacheService sharedInstance]fetchHeaderWithHeaderName:commentModel.head_pic];
    if (image) {
        self.headerImageView.image = image;
    }else {
        [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:self.placeHold completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (image) {
                image = [UIImage circleImage:image];
                self.headerImageView.image = image;
                [[MBCacheService sharedInstance]cacheHeaderWithHeaderName:commentModel.head_pic withHeader:image];
                [[SDImageCache sharedImageCache]clearMemory];
            }
        }];
    }

    self.nameLabel.text = commentModel.nickname;
    self.timeLabel.text = [NSString getChatTimeString:commentModel.add_time];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:[UIView lf_sizeFromIphone6:3]];
    NSMutableAttributedString* commentAtt = [[NSMutableAttributedString alloc]initWithString:commentModel.content attributes:@{NSFontAttributeName:[UIFont lf_systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor colorWithR:40 G:40 B:40],NSParagraphStyleAttributeName:paragraphStyle}];
    self.commentLabel.attributedText = commentAtt;
    
    self.imageContantView.imagePathArray = commentModel.img;
}

-(void)setInfoModel:(MBGoodsInfoModel *)infoModel
{
    _infoModel = infoModel;
    
    if (infoModel.goods.point_rate != 0 ) {
        infoModel.point_rate = infoModel.goods.point_rate;
    }
    self.subTitleLabel.text = [NSString stringWithFormat:@"(好评率%d%%)",infoModel.point_rate];
    self.moreLabel.text = [NSString stringWithFormat:@"共%ld条评论",(long)infoModel.goods.comment_count];
}

-(void)handleClickAllCommentButton
{
    [[NSNotificationCenter defaultCenter]postNotificationName:NOTIFICATION_GOODS_DETAIL_SCROLL_COMMENT object:nil userInfo:@{@"isBanner":@(NO)}];
}


@end
